<?php
    //1.引入文件，连接数据库
    require('common/conn.php');

    // 根据id查询文章内容
    if(isset($_GET['id'])){
        //1.拼接sql
        $sql = "select * from article where id={$_GET['id']}";
        //2.执行sql语句
        $result = mysqli_query($link,$sql);
        //3.解析结果集
        $article = mysqli_fetch_assoc($result);

        //4.更新点击率
        $sql2 = "update article set click=click+1 where id=" .$_GET['id'] ;
        //5.执行sql语句
        $result2 = mysqli_query($link,$sql2);
        if(!$result2){
            exit('更新点击率失败');
        }
    }

    // 评论
    if(isset($_POST['submit'])){

        //2.获取所需参数
        $article_id = $_POST['article_id'];
        $content = $_POST['content'];
        $inputtime = time();

        //3.准备sql语句
        $sql = "insert into comment(article_id, content, inputtime) 
              values ('{$article_id}','{$content}','{$inputtime}')";

        //4.执行sql语句
        $result = mysqli_query($link,$sql);

        //5.判断执行结果
        if($result === false){
            exit('添加失败');
        } else {
            echo "<script>alert('评论成功');</script>";
        }

    }

?>

<!DOCTYPE html>
<html>

<head>
    <title>Roem Blog</title>
    <meta charset="utf-8">
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=Edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1">

    <link href="css/bootstrap.css" rel="stylesheet">
    <link href="css/index.css" rel="stylesheet">
</head>

<body>

    <!-- header -->
    <div class="main-header">
        <div id="blogTitle">
            <h2>Roem的博客</h2>
            <h5>Love Coding, Enjoy Life !</h5>
        </div>
        <div class="nav row">
            <div class="col-md-6 col-md-offset-3">
                <div class="row">
                    <div class="col-md-4"><a class="animation" href="index.php">文章 Articles</a></div>
                    <div class="col-md-4"><a class="animation" href="contact.php">留言 Contact</a></div>
                    <div class="col-md-4"><a class="animation" href="about.html">简介 About</a></div>
                </div>
            </div>
        </div>
    </div>

    <!-- main -->
    <div class="content-warp" id="barba-wrapper" aria-live="polite">

        <!-- left -->
        <div class="container barba-container" style="visibility: visible;">
            <div class="row">

                <!--start main content aera-->
                <div class="col-md-9  main-content">
                    <div class="post">

                        <!--start article header aera-->
                        <div class="header">
                            <div class="title">
                                <h2 class="text-center"><?=$article['title']?></h2>
                            </div>
                            <div class="time">编辑于 <?=date('Y-m-d H:i:s', $article['add_time']);?></div>
                            <div style="clear:both;"></div>
                        </div>

                        <div class="footer">
                            <a class="ret animation" href="index.php"><b>&lt;</b> 回到分类: <?=$article['category']?></a>
                        </div>

                        <!--start main article aera-->
                        <div class="article">

                            <p class="para visible-xs" style="color: #666">* 移动设备下, 可左滑手指以查看较宽代码</p>

                            <!-- 文章内容 -->
                            <div class="txt">
                                <p class="para">
                                    <?=$article['content']?>
                                </p>
                                <blockquote>
                                    <form action="" method="post">
                                        <p class="para-quote">
                                            <textarea name="content"  cols="60" rows="10"></textarea>
                                        </p>
                                        <input type="hidden" name="article_id" value="<?=$article['id']?>">
                                        <input type="submit" name="submit" value="评论"/>
                                    </form>
                                </blockquote>

                            </div>
                            <!--文章内容结束-->
                        </div>

                        <!--start article footer aera-->
                        <div class="footer">
                            <p class="copyinfo">*如非说明，文章均为原创，转载需注明出处.</p>
                            <div style="clear: both;"></div>
                        </div>
                    </div>
                </div>


                <!-- right -->
                <?php require_once "right.php"; ?>
            </div>
        </div>
    </div>


    <!-- footer -->
    <div class="main-footer">
        <div class="container">
            <div class="logo">
                Roem BLOG
            </div>
            <p>&nbsp;</p>
        </div>

        <h5 class="copyright text-center">
            © 2017 &nbsp;|&nbsp; &copy; by Roem
            <a href="admin/login.php" class="no-barba no-barba-prefetch">后台管理</a>
        </h5>
    </div>
</body>

</html>